package com.simple.admin.controller.system;

import com.simple.framework.constant.HttpStatus;
import com.simple.framework.core.controller.BaseController;
import com.simple.framework.core.domain.R;
import com.simple.system.annotation.SystemLog;
import com.simple.system.domain.dto.AddSysMenuDto;
import com.simple.system.domain.entity.SysMenu;
import com.simple.system.domain.vo.SystemManagementRouterVo;
import com.simple.system.enums.BusinessType;
import com.simple.system.service.SysMenuService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 菜单管理
 * @Author xiaoke
 * @Date 2024/8/10 18:00
 * @Version 1.0
 */
@RestController
@RequestMapping("/sys_menu")
public class SysMenuController extends BaseController {

    private final SysMenuService sysMenuService;

    public SysMenuController(SysMenuService sysMenuService) {
        this.sysMenuService = sysMenuService;
    }

    /**
     * 获取当前用户菜单路由
     * @return List<SystemManagementRouterVo>
     */
    @GetMapping("managementRouter")
    public R<List<SystemManagementRouterVo>> managementRouter() {
        return success(sysMenuService.managementRouter());
    }

    /**
     * 获取菜单导航-树状形
     * @return List<SysMenu>
     */
    @GetMapping("/menu")
    public R<List<SysMenu>> menu() {
        List<SysMenu> sysMenus = sysMenuService.listMenu();
        return success(sysMenus);
    }

    /**
     * 获取菜单权限-树状形
     * @return List<SysMenu>
     */
    @GetMapping("/perms")
    public R<List<SysMenu>> menuPerms() {
        List<SysMenu> sysMenusPerms = sysMenuService.listMenuPerms();
        return success(sysMenusPerms);
    }

    /**
     * 新增菜单
     * @param sysMenu 新增参数
     * @return Long
     */
    @SystemLog(title = "新增菜单",businessType = BusinessType.INSERT)
    @PostMapping
    public R<Long> create(@RequestBody AddSysMenuDto sysMenu) {
        return success(HttpStatus.CREATED,sysMenuService.create(sysMenu));
    }

    /**
     * 修改菜单
     * @param sysMenu 修改参数
     * @return Void
     */
    @SystemLog(title = "修改菜单",businessType = BusinessType.UPDATE)
    @PutMapping("/{id}")
    public R<Void> update(@PathVariable("id") Long id,@RequestBody AddSysMenuDto sysMenu) {
        sysMenuService.update(id,sysMenu);
        return success();
    }

    /**
     * 删除菜单
     * @param id 菜单id
     * @return Void
     */
    @SystemLog(title = "删除菜单",businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> delete(@PathVariable("id") Long id) {
        sysMenuService.delete(id);
        return success();
    }
}
